package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ApArticleService;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.dtos.NewsPageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 陈辉
 * @data 2024 11:20
 */
@Component
@Slf4j
public class ArticleListener {

    @Autowired
    private ApArticleService apArticleService;

    @KafkaListener(topics = "article_down_or_up")
    public void downOrUp(String jsonData){
        Map map = JSON.parseObject(jsonData, Map.class);
        log.info("app端文章上下架操作监听到消息：{}",map);
        apArticleService.downOrUp(map);
    }

    @KafkaListener(topics = "article_comment")
    public void updateComment(String msg){
        log.info("接收修改文章评论量信息，code={}",msg);
        Long articleId = JSON.parseObject(msg, Long.class);
        ApArticle article = apArticleService.getById(articleId);
        Integer comment = article.getComment();
        if (comment == null) {
            comment = 0;
        }
        article.setComment(comment+1);
        apArticleService.updateById(article);
    }

    @KafkaListener(topics = "article_list")
    public void getArticlelist(String msg){
        log.info("收到分页展示文章列表消息，{}",msg);
        NewsPageDto newsPageDto = JSON.parseObject(msg, NewsPageDto.class);
        apArticleService.newsPage(newsPageDto);
    }
}
